﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using DTO;
namespace BUS
{
    public class ProductBUS
    {

        public static bool Update(PRODUCT m_Product)
        {
            try
            {
                bool isExits = ProductDAO.IsExisting(m_Product.IDPRODUCT);
                if (!isExits)
                    return false;
                return ProductDAO.Update(m_Product);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<PRODUCT> GetAllProduct()
        {
            try
            {
                return ProductDAO.GetAllProduct();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static PRODUCT GetProduct(int IDProduct)
        {
            try
            {
                if (!ProductDAO.IsExisting(IDProduct))
                    return null;
                return ProductDAO.GetProduct(IDProduct);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lay danh sach san pham còn khuyến mãi de show ra man hinh (lay tren toan quoc)
        /// </summary>
        /// <returns></returns>
        public static List<PRODUCT> GetProductStillSale()
        {
            try
            {
                List<PRODUCT> ListProduct = ProductDAO.GetProductToShow();
                SapXep(ListProduct);
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lay danh sach san pham còn khuyến mãi de show ra theo city (hn ,hcm)
        /// </summary>
        /// <param name="City"></param>
        /// <returns></returns>
        public static List<PRODUCT> GetProductStillSaleByCity(string City)
        {
            try
            {
               List<PRODUCT> ListProduct= ProductDAO.GetProductToShowByCity(City);
                SapXep(ListProduct);
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// lay danh sach san pham đã đăng (đã kết thúc) trên toàn quốc
        /// </summary>
        /// <returns></returns>
        public static List<PRODUCT> GetProductEnded()
        {
            try
            {
                List<PRODUCT> ListProduct = ProductDAO.GetProductEnded();
                SapXep(ListProduct);
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy danh sách sản phẩm đã kết thúc theo từng thành phố
        /// </summary>
        /// <param name="City"></param>
        /// <returns></returns>
        public static List<PRODUCT> GetProductEndedByCity(string City)
        {
            try
            {
                List<PRODUCT> ListProduct = ProductDAO.GetProductEndedByCity(City);
                SapXep(ListProduct);
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy danh sách sản phẩm sắp được khuyến mãi (trên toàn quốc)
        /// </summary>
        /// <returns></returns>
        public static List<PRODUCT> GetProductSapKhuyenMai()
        {
            try
            {
                List<PRODUCT> ListProduct = ProductDAO.GetProductSapKhuyenMai();
                SapXep(ListProduct);
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy danh sách sản phẩm sắp được khuyến mãi theo thanh pho
        /// </summary>
        /// <param name="City"></param>
        /// <returns></returns>
        public static List<PRODUCT> GetProductSapKhuyenMai(string City)
        {
            try
            {
                List<PRODUCT> ListProduct = ProductDAO.GetProductSapKhuyenMai(City);
                SapXep(ListProduct);
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Insert(PRODUCT Product)
        {
            try
            {
                Product.OrderedNumber = 0;
                Product.MIN_NUMBER = 0;
                Product.PurchasedNumber = 0;
                Product.IMAGE = "images\\Products\\product" + 1 + "\\";
                if (Product.STATUS==null || Product.STATUS != 1)
                {
                    Product.STATUS = 0;
                }
                Product.VISIBLE = true;
                return ProductDAO.Insert(Product);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private static string ChuanHoaChuoi(string stringSearch)
        {
            for (int i = 0; i < stringSearch.Length-1; ++i)
            {
                if ((stringSearch[i].CompareTo(' ') == 0) && (stringSearch[i + 1].CompareTo(' ') == 0))
                {
                    stringSearch.Remove(i, 1);
                    --i;
                }
            }
            return stringSearch.ToLower();
        }
        public static List<PRODUCT> Search(string stringSearch)
        {
            try
            {
                if (stringSearch.Trim() == "")
                    return null;
                stringSearch = ChuanHoaChuoi(stringSearch);
                return ProductDAO.Search(stringSearch.Trim());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Delete(int IDProduct)
        {
            try
            {
                bool isExist = ProductDAO.IsExisting(IDProduct);
                if (!isExist)
                    return false;
                return ProductDAO.Delete(IDProduct);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy sản phẩm chưa được duyệt
        /// </summary>
        /// <returns></returns>
        public static List<PRODUCT> GetProductNotCheck()
        {
            try
            {
                List<PRODUCT> ListProduct = ProductDAO.GetProductNotCheck();
                SapXep(ListProduct);
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy sản phẩm chưa được duyệt theo thành phố
        /// </summary>
        /// <param name="City"></param>
        /// <returns></returns>
        public static List<PRODUCT> GetProductNotCheck(string City)
        {
            try
            {
                List<PRODUCT> ListProduct = ProductDAO.GetProductNotCheck(City);
                SapXep(ListProduct);
                return ListProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// sap xep nhug san pham co time start gan voi today nhat
        /// </summary>
        /// <param name="ListProduct"></param>
        private static void SapXep(List<PRODUCT> ListProduct)
        {
            PRODUCT TEMP = new PRODUCT();
            for (int i = 0; i < ListProduct.Count-1; ++i)
            {
                for (int j = i; j < ListProduct.Count; ++j)
                {
                    double temp1=Math.Abs((ListProduct[i].TIME_START.Value-DateTime.Today).TotalHours);
                    double temp2=Math.Abs((ListProduct[j].TIME_START.Value-DateTime.Today).TotalHours);
                    if (temp1 > temp2)
                    {
                        TEMP = ListProduct[i];
                        ListProduct[i] = ListProduct[j];
                        ListProduct[j] = TEMP;
                    }
                }
            }
        }

        
        /// <summary>
        /// duyet sap pham (set status = 1)
        /// </summary>
        /// <param name="id"></param>
        public static void ApproveProduct(int id)
        {
            try
            {
                bool isExist = ProductDAO.IsExisting(id);
                    if(!isExist)
                        return;
                    ProductDAO.ApproveProduct(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// cộng thế số đơn đặt hàng trong product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="quality"></param>
        public static bool AddOrderNumber(int id, int quality)
        {
            try
            {
                bool isExist = ProductDAO.IsExisting(id);
                if (!isExist)
                    return false;
                return ProductDAO.AddOrderNumber(id, quality);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
